package org.gw.ylc.base.model;

import org.gw.ylc.base.service.DepartmentService;
import org.gw.ylc.base.service.MajorService;
import org.hibernate.annotations.Subselect;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import java.io.Serializable;

/**
 * 用户视图<br/>
 * userId varchar 40 是 用户/登录名（取学生表学号、管理员表工号）<br>
 * name varchar 40 是 姓名<br>
 * department int 10 是 院系<br>
 * major int 10 是 专业 –学生用<br>
 * grade int 10 是 年级 -学生用<br>
 * type int 1 是 0学生/老师 1院系管理员2学校管理员3、系统管理员<br>
 * status int 1 是 0有效1无效<br>
 */
@Entity
@Table(name = "userInfo")
@Subselect("SELECT s.id as id, s.xh AS uid, s.name AS name, s.department_id AS department, "
		+ " s.major_id AS major, s.grade AS grade,'' AS zc, 0 AS TYPE, s.status AS STATUS,null as app,0 as teacher, e.password "
		+ " FROM Student s LEFT JOIN Userinfoext e ON s.xh = e.userId UNION ALL "
		+ "(SELECT t.id as id, t.employeeNo AS uid, t.name AS name, t.department_id AS department, "
		+ " '' AS major, '' AS grade,zc.mc AS zc, 0 AS TYPE, t.status AS STATUS,null as app,1 as teacher, e.password "
		+ " FROM Teacher t LEFT JOIN Userinfoext e ON t.employeeNo = e.userId LEFT JOIN t_bzgb_zyjszw zc ON t.zcdm=zc.code) UNION ALL "
		+ "(SELECT a.id as id, a.employeeNo AS uid, a.name AS name, a.manaDep AS department, "
		+ " '' AS major, '' AS grade,'' AS zc, a.type AS TYPE, a.status AS STATUS,a.app,1 as teacher, e.password "
		+ " FROM administrator a LEFT JOIN userinfoext e ON a.employeeNo = e.userId)")
public class UserInfo implements Serializable {
	private static final long serialVersionUID = 2137877614335163374L;

	@Id
	private String id;// 无意义唯一标识

	private String uid;// 学生学号或者工号等

	private String name;

	private String department;

	private String major;

	private String grade;
	
	private String zc;

	private int type = -1;// 0普通用户1院系管理员2学校管理员3、系统管理员

	private int status = 0;// 0无效1有效
	private int teacher = 0;// 0学生，1老师
	private String password;// 从user info ext中获得

	private String app;// 应用 如 第二课堂 ,青共校
	public static String ANONYMOUS_UID = "ANONYMOUS";
	public static String ANONYMOUS_NAME = "游客";

	public static UserInfo ANONYMOUS = new UserInfo(ANONYMOUS_UID, ANONYMOUS_UID, ANONYMOUS_NAME);

	private UserInfo(String id, String uid, String name) {
		this.id = id;
		this.uid = uid;
		this.name = name;
	}

	public boolean isAnonymous() {
		return this.uid.equals(ANONYMOUS_UID);
	}

	public UserInfo() {

	}

	public UserInfo(String id, String uid, String name, String department, String major, String grade, int type,
			int status, String password) {
		super();
		this.id = id;
		this.uid = uid;
		this.name = name;
		this.department = department;
		this.major = major;
		this.grade = grade;
		this.type = type;
		this.status = status;
		this.password = password;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}
	
	@Transient
	public String getDepartmentName() {
		if (department == null) {
			return "";
		}
		Department dep = DepartmentService.getINSTANCE().findById(department);
		if (dep != null) {
			return dep.getName();
		}
		return "";
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	@Transient
	public String getMajorname() {
		if (major == null) {
			return "";
		}
		Major m = MajorService.getINSTANCE().findById(major);
		if (m != null) {
			return m.getName();
		}
		return "";
	}
	
	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	/**
	 * 0学生1院系管理员2学校管理员3、系统管理员
	 * 
	 * @return
	 */
	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getApp() {
		return app;
	}

	public int getTeacher() {
		return teacher;
	}

	public void setTeacher(int teacher) {
		this.teacher = teacher;
	}

	public boolean isTeacherRole() {
		return type == 0 && teacher == 1;
	}

	public boolean isStudentRole() {
		return type == 0 && teacher == 0;
	}

	public void setApp(String app) {
		this.app = app;
	}
	
	public void setZc(String zc) {
		this.zc = zc;
	}

	public String getZc() {
		return zc;
	}
}
